package com.tianzeculture.stagebao.activity.business;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.home.adapter.NoticeAdapter;
import com.tianzeculture.stagebao.base.BaseFragment;
import com.tianzeculture.stagebao.bean.NoticeList;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.bean.User;
import com.tianzeculture.stagebao.net.BusinessRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.view.MyListView;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/27 0027.
 */

public class BusinessDataFragment extends BaseFragment {
    @BindView(R.id.lvHistory)
    MyListView lvHistory;
    @BindView(R.id.tvContent)
    TextView tvContent;
    NoticeAdapter noticeAdapter = null;
    List<NoticeList> joblist;

    @Request
    BusinessRestUsage businessRestUsage = null;
    private int toSellerdetails = 101;//查看商家详情

    @Override
    public void initUI() {
        super.initUI();
        lvHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), NoticeDetailActivity.class);
                intent.putExtra("notice", (Serializable) joblist.get(i));
                intent.putExtra("flag", true);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        showProgressDialog();
        businessRestUsage.toSellerdetails(toSellerdetails, getActivity().getIntent().getStringExtra("userId"));
    }

    private FragmentInteraction listterner;

    public interface FragmentInteraction {
        void process(User user);
    }

    // 当FRagmen被加载到activity的时候会被回调
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof FragmentInteraction) {
            listterner = (FragmentInteraction) activity; // 2.2 获取到宿主activity并赋值
        } else {
            throw new IllegalArgumentException("activity must implements FragmentInteraction");
        }
    }


    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == toSellerdetails) {
            dismissProgressDialog();
            if (msg.getData()) {
                ResultBean resultBean = (ResultBean) msg.getObj();
                if (resultBean != null) {
                    User user = resultBean.getUser();
                    listterner.process(user);
                    tvContent.setText(user.getCdesc());
                }
                joblist = resultBean.getJoblist();
                noticeAdapter = new NoticeAdapter(getContext(), joblist, null);
                lvHistory.setAdapter(noticeAdapter);
            }
        }
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_business_data;
    }
}
